import {Message } from 'element-ui'
let token = '';

if(localStorage.user){
  //  token = JSON.parse(localStorage.user).token;

    //如果存在监控用户信息 并且打开的是监控地图
    if(localStorage.monitorUser&&window.location.hash=='#/map'){
        token = JSON.parse(localStorage.getItem('monitorUser')).token
    }else{
        token = JSON.parse(localStorage.getItem('user')).token
    }
}


const ws = {
    wsServer:"ws://106.14.249.170:83/websocket/"+token,
    //wsServer:"ws://"+window.location.hostname+":8085/websocket/"+token,
    websocket:null,
    connection:false,//是否连接成功
    interval:null,//重连定时器
    init:function (val) {
        var self = val;
        this.websocket = new WebSocket(this.wsServer);
        this.websocket.onopen = function (evt) {
            ws.connection=true;
        };
        this.websocket.onclose = function (evt) {
            if(ws.connection){
              Message({message:'通信断开',type: 'error'});
              ws.reconnection();
            }

        };
        this.websocket.onmessage = function (evt) {
                if(evt.data){
                    let socketMsg = JSON.parse(evt.data)
                     //console.log("socketMsg"+socketMsg);
                    if(socketMsg.positions){
                      // console.log("位置改变"+socketMsg);
                          self.$store.dispatch('updateDevicePosition',socketMsg.positions);
                    }else if(socketMsg.devices){
                          self.$store.dispatch('updateDeviceStatus', socketMsg.devices);
                    }else if(socketMsg.states){
                        // console.log("updateDeviceState"+socketMsg.states);
                        //  self.$store.dispatch('updateDeviceState', socketMsg.states);
                    }
                }
        };
        this.websocket.onerror = function (evt) {
        };
    },
    reconnection:function (val) {
        ws.connection=false;
        ws.interval =  setInterval(function () {
                      if (ws.connection) {
                          Message({message:'重连成功',type: 'success'});
                          clearInterval(ws.interval);
                      } else {
                          Message({message:'尝试重连',type: 'warning'});
                          ws.connection=false;
                          ws.init(val);
                      }
                  }, 10000);
    }
}

export default ws
